package com.cheyun.netsalev3.view

import android.content.Intent
import android.util.Log
import android.view.View
import android.view.animation.AlphaAnimation
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.HomePageAdapter
import com.cheyun.netsalev3.bean.CustomListParam
import com.cheyun.netsalev3.databinding.ActivitySneakGuestDataBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.view.home.CustomerItemFragment
import com.cheyun.netsalev3.viewmodel.CustomerItemFragmentViewModel
import com.google.android.material.tabs.TabLayout

class SneakGuestListActivity : BaseActivity<ActivitySneakGuestDataBinding>() {


    override fun initContentView(): Int {
        return R.layout.activity_sneak_guest_data
    }

//    var titles = arrayListOf("全部", "转出", "战败", "无效")
//    var nkeys = arrayListOf("archivepot.index", "archivepot.discard", "archivepot.failure", "archivepot.invalid")
    var titles = arrayListOf("全部", "战败", "无效")
    var nkeys = arrayListOf("archivepot.index", "archivepot.failure", "archivepot.invalid")

    private var listFragmet = ArrayList<Fragment>()
    override fun initViewModel(): ViewModel? {
        val factory = InjectorUtils.getCustomerItemFragmentViewModelFactory(nkeys,boolean = true)
        return ViewModelProviders.of(this, factory).get(CustomerItemFragmentViewModel::class.java)
    }

    override fun initData() {
    }

    override fun initView() {
        for (i in titles.indices) {
            listFragmet.add(CustomerItemFragment.newInstance(nkeys[i]))
        }
        val pagerAdapter = HomePageAdapter(supportFragmentManager, listFragmet)
        pagerAdapter.titles = titles
        binding.viewPager.offscreenPageLimit = 3
        binding.viewPager.adapter = pagerAdapter
        binding.tabLayout.setupWithViewPager(binding.viewPager)
        //选中的位置
        binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(p0: TabLayout.Tab?) {

            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {
            }

            override fun onTabSelected(p0: TabLayout.Tab?) {
                var pos = binding.tabLayout.selectedTabPosition
                var nke = nkeys[pos]
                binding.viewModel?.totlelist!![nke]?.observe(this@SneakGuestListActivity, Observer {
                    binding.totleText.text = titles[pos] + it.toString() + "人"
                    showTips()
                })
            }
        })
        var nke = nkeys[0]
        binding.viewModel?.totlelist!![nke]?.observe(this@SneakGuestListActivity, Observer {
            binding.totleText.text = titles[0] + it.toString() + "人"
            showTips()
        })
        binding.searchBut.setOnClickListener {
            //高级搜索
            var intent = Intent(this, AdvancedSearchActivity::class.java)
            intent.putExtra("nkey", "archivepot.index")
//            intent.putExtra("nkey", "archive.index")
            startActivity(intent)
        }
        binding.searchText.setOnClickListener {
            var intent = Intent(this, SearchActivity::class.java)
            intent.putExtra("nkey", "archivepot.index")
//            intent.putExtra("nkey", "archive.index")
            startActivity(intent)
        }
        binding.leftBut.setOnClickListener{
            finish()
        }


    }
    private fun showTips(){
        binding.totleText.setVisibility(View.VISIBLE)
        binding.totleText.clearAnimation()
        val animation = AlphaAnimation(1f, 0f)
        animation.duration = 3000
        binding.totleText.startAnimation(animation)
        binding.totleText.setVisibility(View.GONE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == 101) {
            var list = data?.getSerializableExtra("poplist") as List<CustomListParam>
            binding.viewModel?.onResult(list)
            for (fragment in listFragmet) {
                fragment?.onActivityResult(requestCode, resultCode, data)
            }
            Log.e("101....", list.toString())
        }
    }

}
